package com.maisuiit.pattern.singleton;

/**
 * 使用枚举实现单例模式 优点:实现简单、枚举本身就是单例，由jvm从根本上提供保障!避免通过反射和反序列化的漏洞 缺点没有延迟加载
 * @author zhangcc
 * @date 2021-07-13
 */
public class SingletonFour {
    public static SingletonFour getInstance() {
        return SingletonEnumFour.INSTANCE.getInstance();
    }
    //定义枚举
    private static enum SingletonEnumFour {
        INSTANCE;
        // 枚举元素为单例
        private SingletonFour singletonFour;

        private SingletonEnumFour() {
            System.out.println("枚举SingletonEnumFour私有构造参数");
            singletonFour = new SingletonFour();
        }

        public SingletonFour getInstance() {
            return singletonFour;
        }
    }
}
